package com.dayuanit.dy15.atm.controller;

import com.dayuanit.dy15.atm.dto.CardDTO;
import com.dayuanit.dy15.atm.dto.ResponseDTO;
import com.dayuanit.dy15.atm.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/card")
public class CardController extends BaseController {

    /**
     * @Autowired 按照类型注入的，但是如果接口有多个子类，就会报错。
     * 解决方案一：加上@Qualifier来指定名字
     *
     * 解决方案二：@Resource，可以按照名字 也可以按照类型来指定某个bean对象
     *
     */
    @Autowired
    @Qualifier("cardServiceTransferImpl")
//    @Resource(name="cardServiceTransferImpl", type = CardService.class)
    private CardService cardService;

//    @RequestMapping(value = "/card/openaccount", method = RequestMethod.POST)
    @PostMapping("/openaccount")
    public ResponseDTO openaccount(@RequestParam("password") String pwd, String confirmPassword, HttpServletRequest request) {
        cardService.openaccount(pwd, confirmPassword, getUserId(request));
        return ResponseDTO.sucess();
    }


    /**
     * 存款
     * @param cardId
     * @param password
     * @param amount
     * @return
     */
    @PostMapping("/deposit")
    public ResponseDTO deposit(long cardId, String password, String amount, HttpServletRequest request) {
        cardService.deposit(cardId, password, amount, getUserId(request));
        return ResponseDTO.sucess();
    }

    /**
     * 取钱
     * @param cardId
     * @param password
     * @param amount
     * @param request
     * @return
     */
    @RequestMapping(value = "/draw")
    public ResponseDTO draw(long cardId, String password, String amount, HttpServletRequest request) {
        cardService.draw(cardId, password, amount, getUserId(request));
        return ResponseDTO.sucess();
    }

    /**
     * 转账
     * @param cardId
     * @param password
     * @param amount
     * @param cardNumIn
     * @param request
     * @return
     */
    @RequestMapping("/transfer")
    public ResponseDTO transfer(long cardId, String password, String amount, String cardNumIn, HttpServletRequest request) {
        cardService.transfer(cardId, password, amount, cardNumIn, getUserId(request));
        return ResponseDTO.sucess();
    }

    /**
     * 我的银行卡
     * @param request
     * @return
     */
    @RequestMapping(value = "/loadMyCard", method = RequestMethod.GET)
    public ResponseDTO loadMyCard(HttpServletRequest request) {
        return ResponseDTO.sucess(cardService.listMyCard(getUserId(request)));
    }

    @RequestMapping("/listOrders")
    public ResponseDTO listOrders(@RequestParam(required = false, defaultValue = "1") int currentPage,
                                  @RequestParam(required = false, defaultValue = "2") int pageSize,
                                  HttpServletRequest request) {
        return ResponseDTO.sucess( cardService.listOrders(getUserId(request), currentPage, pageSize));
    }

    @RequestMapping("/cancelTransfer")
    public ResponseDTO cancelTransfer(long orderId, HttpServletRequest request) {
        cardService.cancelTransfer(getUserId(request), orderId);
        return ResponseDTO.sucess();
    }

}
